package com.bac.bacplatform;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

public class CashToOilActivity extends BaseActivity implements OnClickListener{
	private RelativeLayout rl_addNum,rl_subNum,month_number,rl_agree_it;
	private TextView money_text,
					 cardNum,//卡号 
					 monthNum,//month_number_tv2,下拉框月
					 rl_01_tv02,//每月金额
					 rl_02_tv02,//月数
					 rl_03_tv02,//加送
					 rl_04_tv02,//实际充值
					 rl_tv02,//合计充值
					 discountTextView;//折扣
	private ImageView ischecked;
	private Button btn_recharge;
	private PopupWindow mPopupWindow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cash_oil);
		
		init();
	}

	@Override
	public void init() {
		money_text = (TextView) findViewById(R.id.money_text);
		cardNum = (TextView) findViewById(R.id.card_number);
		monthNum = (TextView) findViewById(R.id.month_number_tv2);
		rl_01_tv02 = (TextView) findViewById(R.id.rl_01_tv02);
		rl_02_tv02 = (TextView) findViewById(R.id.rl_02_tv02);
		rl_03_tv02 = (TextView) findViewById(R.id.rl_03_tv02);
		rl_04_tv02 = (TextView) findViewById(R.id.rl_04_tv02);
		rl_tv02 = (TextView) findViewById(R.id.rl_tv02);
		discountTextView = (TextView) findViewById(R.id.discount_tv02);
		ischecked = (ImageView) findViewById(R.id.ischecked);
		btn_recharge = (Button) findViewById(R.id.go_recharge);
		
		rl_addNum = (RelativeLayout) findViewById(R.id.additive);
		rl_addNum.setOnClickListener(this);
		rl_subNum = (RelativeLayout) findViewById(R.id.subtraction);
		rl_subNum.setOnClickListener(this);
		month_number = (RelativeLayout) findViewById(R.id.month_number);
		month_number.setOnClickListener(this);
		rl_agree_it = (RelativeLayout) findViewById(R.id.agree_it);
		rl_agree_it.setOnClickListener(this);
		

	}

	@SuppressLint("NewApi") @Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.additive:
			AddNumber();
			break;
		case R.id.subtraction:
			SubNumber();
			break;
		case R.id.month_number:
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
				return;
			}else {
				initmPopupWindowView();
				mPopupWindow.showAsDropDown(v, 0, 0);
			}
			break;
		case R.id.agree_it:
			ischecked.setVisibility(View.VISIBLE);
			btn_recharge.setBackground(getResources().getDrawable(R.drawable.btn_on));
			btn_recharge.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					goToRecharge();
					
				}
			});
			break;
		default:
			break;
		}
		
	}
	private void goToRecharge(){
		String kahao = cardNum.getText().toString();
		String jine = money_text.getText().toString();
		String yueshu = monthNum.getText().toString();
		String zhekou = discountTextView.getText().toString();
		String key = "CashToOilActivity";
		Intent intentToConfirm = new Intent(CashToOilActivity.this,ConfirmRecharge.class);
		Bundle bundle = new Bundle();
		bundle.putString("cardNum", kahao);
		bundle.putString("money_text", jine);
		bundle.putString("monthNum", yueshu);
		bundle.putString("discountTextView", zhekou);
		bundle.putString("whichActivity", key);
		intentToConfirm.putExtras(bundle);
		startActivity(intentToConfirm);
	}
		//通过下拉框选择充值月数
		private void initmPopupWindowView() {
			View customView = getLayoutInflater().inflate(R.layout.choosenumber, null);
			mPopupWindow = new PopupWindow(customView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			mPopupWindow.setFocusable(true);
			customView.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (mPopupWindow != null && mPopupWindow.isShowing()) {
						mPopupWindow.dismiss();
						mPopupWindow = null;
					}
					return false;
				}
			});
			TextView textView1 = (TextView)customView.findViewById(R.id.number_tv01);
			final String tv01 = textView1.getText().toString();
			textView1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv01);
					initPay(1);
					mPopupWindow.dismiss();
				}
			});
			TextView textView2 = (TextView)customView.findViewById(R.id.number_tv02);
			final String tv02 = textView2.getText().toString();
			textView2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv02);
					initPay(2);
					mPopupWindow.dismiss();
				}
			});
			TextView textView3 = (TextView)customView.findViewById(R.id.number_tv03);
			final String tv03 = textView3.getText().toString();
			textView3.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv03);
					initPay(3);
					mPopupWindow.dismiss();
				}
			});
			TextView textView4 = (TextView)customView.findViewById(R.id.number_tv04);
			final String tv04 = textView4.getText().toString();
			textView4.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv04);
					initPay(4);
					mPopupWindow.dismiss();
				}
			});
			TextView textView5 = (TextView)customView.findViewById(R.id.number_tv05);
			final String tv05 = textView5.getText().toString();
			textView5.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv05);
					initPay(5);
					mPopupWindow.dismiss();
				}
			});
			TextView textView6 = (TextView)customView.findViewById(R.id.number_tv06);
			final String tv06 = textView6.getText().toString();
			textView6.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					monthNum.setText(tv06);
					initPay(6);
					mPopupWindow.dismiss();
				}
			});
			
		}
		
		//选择月数后改变金额
		private void initPay(int i){
			String moneyString = money_text.getText().toString();
			String discount = discountTextView.getText().toString();
			int num1 = Integer.parseInt(moneyString);
			int num2 = Integer.parseInt(discount);
			rl_02_tv02.setText(i+"");
			rl_04_tv02.setText(num1*i+"");
			rl_tv02.setText(num1*i+num1*num2*i/100+"");
		}
		
	private void AddNumber(){
		String moneyString = money_text.getText().toString();
		String month = monthNum.getText().toString();
		String discount = discountTextView.getText().toString();
		
		int num1 = Integer.parseInt(moneyString);
		int mon = Integer.parseInt(month);
		int dis = Integer.parseInt(discount);
		num1 += 100;
		rl_01_tv02.setText(num1+"");
		rl_02_tv02.setText(mon+"");
		rl_03_tv02.setText(num1*dis/100+"");
		rl_04_tv02.setText(num1*mon+"");
		rl_tv02.setText((num1*mon+mon*num1*dis/100)+"");
		money_text.setText(num1+"");
		
	}
	private void SubNumber(){
		String moneyString = money_text.getText().toString();
		String month = monthNum.getText().toString();
		String discount = discountTextView.getText().toString();
		int mon = Integer.parseInt(month);
		int dis = Integer.parseInt(discount);
		int num1 = Integer.parseInt(moneyString);
		if (num1 > 0) {
			num1 -= 100;
			money_text.setText(num1+"");
			rl_02_tv02.setText(mon+"");
			rl_03_tv02.setText(num1*dis/100+""); 
			rl_04_tv02.setText(num1*mon+"");
			rl_tv02.setText((num1*mon+mon*num1*dis/100)+"");
			money_text.setText(num1+"");
		}
	}

}
